import { Component, OnInit, Input } from '@angular/core';
import { ModalController } from '@ionic/angular';
@Component({
  selector: 'app-publish-house-type',
  templateUrl: './publish-house-type.component.html',
  styleUrls: ['./publish-house-type.component.scss'],
})
export class PublishHouseTypeComponent implements OnInit {

  @Input() tagData: any;
  rowListData = [];
  returnData =
    {
      // tslint:disable-next-line:object-literal-key-quotes
      'roomType': { key: 'room_type', value: '0', label: '' },
      // tslint:disable-next-line:object-literal-key-quotes
      'roomHall': { key: 'room_hall', value: '0', label: '' },
      // tslint:disable-next-line:object-literal-key-quotes
      'roomBit': { key: 'room_bit', value: '0', label: '' }
    };
  constructor(
    public modalController: ModalController,
  ) { }

  ngOnInit() {
    if (this.tagData && this.tagData.subHtmlObj) {
      const array = this.tagData.subHtmlObj;
      const roomTypeList = array[0];
      const roomHallList = array[1];
      const roomBitList = array[2];
      // tslint:disable-next-line:prefer-for-of
      for (let index = 0; index < roomTypeList.data.length; index++) {
        // const element = array[index];
        if (this.tagData.roomType === roomTypeList.data[index].value) {
          // tslint:disable-next-line:no-string-literal
          this.returnData['roomType'].value = roomTypeList.data[index].value;
          // tslint:disable-next-line:no-string-literal
          this.returnData['roomType'].label = roomTypeList.data[index].label;
        }
        if (this.tagData.roomHall === roomHallList.data[index].value) {
          // tslint:disable-next-line:no-string-literal
          this.returnData['roomHall'].value = roomHallList.data[index].value;
          // tslint:disable-next-line:no-string-literal
          this.returnData['roomHall'].label = roomHallList.data[index].label;
        }
        if (this.tagData.roomBit === roomBitList.data[index].value) {
          // tslint:disable-next-line:no-string-literal
          this.returnData['roomBit'].value = roomBitList.data[index].value;
          // tslint:disable-next-line:no-string-literal
          this.returnData['roomBit'].label = roomBitList.data[index].label;
        }
        this.rowListData.push(
          {
            roomType:
            {
              label: roomTypeList.data[index].label,
              value: roomTypeList.data[index].value,
              formKey: roomTypeList.formKey,
              isChecked:
                this.tagData.roomType ?
                  (this.tagData.roomType === roomTypeList.data[index].value) ? 'true' : 'false' : ' false'
            },
            roomHall:
            {
              label: roomHallList.data[index].label,
              value: roomHallList.data[index].value,
              formKey: roomHallList.formKey,
              isChecked:
                this.tagData.roomHall ?
                  (this.tagData.roomHall === roomHallList.data[index].value) ? 'true' : 'false' : ' false'
            },
            roomBit:
            {
              label: roomBitList.data[index].label,
              value: roomBitList.data[index].value,
              formKey: roomBitList.formKey,
              isChecked:
                this.tagData.roomBit ?
                  (this.tagData.roomBit === roomBitList.data[index].value) ? 'true' : 'false' : ' false'
            }
          }
        );
      }

      // console.log(this.rowListData);
    } else if (this.tagData && this.tagData.subData) {
      // 另外一种方式解析数据 用于search-filter 住宅出租信息筛选
      const roomTypeList = this.tagData.data; // room
      roomTypeList.formKey = this.tagData.formKey;
      const roomHallList = this.tagData.subData[0].itemList;
      roomHallList.formKey = this.tagData.subData[0].formKey;
      const roomBitList = this.tagData.subData[1].itemList;
      roomBitList.formKey = this.tagData.subData[1].formKey;
      // tslint:disable-next-line:prefer-for-of
      for (let index = 0; index < roomTypeList.length; index++) {
        if (this.tagData.roomType === roomTypeList[index].value) {
          // tslint:disable-next-line:no-string-literal
          this.returnData['roomType'].value = roomTypeList[index].value;
          // tslint:disable-next-line:no-string-literal
          this.returnData['roomType'].label = roomTypeList[index].label;
        }
        if (this.tagData.roomHall === roomHallList[index].value) {
          // tslint:disable-next-line:no-string-literal
          this.returnData['roomHall'].value = roomHallList[index].value;
          // tslint:disable-next-line:no-string-literal
          this.returnData['roomHall'].label = roomHallList[index].label;
        }
        if (this.tagData.roomBit === roomBitList[index].value) {
          // tslint:disable-next-line:no-string-literal
          this.returnData['roomBit'].value = roomBitList[index].value;
          // tslint:disable-next-line:no-string-literal
          this.returnData['roomBit'].label = roomBitList[index].label;
        }
        // const element = array[index];
        this.rowListData.push(
          {
            roomType:
            {
              label: roomTypeList[index].label,
              value: roomTypeList[index].value,
              formKey: roomTypeList.formKey,
              isChecked:
                this.tagData.roomType ?
                  (this.tagData.roomType === roomTypeList[index].value) ? 'true' : 'false' : ' false'
            },
            roomHall:
            {
              label: roomHallList[index].label,
              value: roomHallList[index].value,
              formKey: roomHallList.formKey,
              isChecked:
                this.tagData.roomHall ?
                  (this.tagData.roomHall === roomHallList[index].value) ? 'true' : 'false' : ' false'
            },
            roomBit:
            {
              label: roomBitList[index].label,
              value: roomBitList[index].value,
              formKey: roomBitList.formKey,
              isChecked:
                this.tagData.roomBit ?
                  (this.tagData.roomBit === roomBitList[index].value) ? 'true' : 'false' : ' false'
            }
          }
        );
      }
    }


  }

  onDismiss() {
    this.modalController.dismiss();
  }

  onOk() {
    this.modalController.dismiss({ dismissed: true, itemdata: this.returnData });
  }

  buttonClick(label, value, type, id) {
    const array = document.getElementsByClassName(type);
    // tslint:disable-next-line:prefer-for-of
    for (let index = 0; index < array.length; index++) {
      const element = array[index];
      if (type + id === element.id) {
        this.returnData[type].value = value;
        this.returnData[type].label = label;
        this.addClass(element, "btn-checked");
      } else {
        this.removeClass(element, "btn-checked");
      }
    }
  }

  /**
   * 添加class
   */
  addClass(ele, value) {
    if (value) {
      let curValue = this.getClass(ele);
      if (curValue.indexOf(value) === -1) {
        curValue = curValue + " " + value;
      }
      ele.setAttribute("class", curValue.trim());
    }
  }
  /**
   * 删除class
   */
  removeClass(ele, value) {
    if (value) {
      let curValue = this.getClass(ele);
      if (curValue.indexOf(value) > -1) {
        curValue = (" " + curValue + " ").replace(" " + value + " ", " ");
      }
      ele.setAttribute("class", curValue.trim());
    }
  }
  /**
   * 元素是否存在值为@{value}的class
   */
  hasClass(ele, value) {
    let has = false;
    if (value) {
      const curValue = this.getClass(ele);
      if (curValue.indexOf(value) > -1) {
        has = true;
      }
    }
    return has;
  }

  /**
   * 获取class
   */
  getClass(elem) {
    return elem.getAttribute && elem.getAttribute("class") || "";
  }
}
